package com.softtown.modules.organization.controller;

import com.softtown.common.annotation.LogOperation;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.regularUtils.RegularUtils;
import com.softtown.common.utils.Result;
import com.softtown.common.validator.ValidatorUtils;
import com.softtown.common.validator.group.DefaultGroup;
import com.softtown.modules.organization.dto.CompOrgDeptDTO;
import com.softtown.modules.organization.dto.CompOrgDeptTreeDTO;
import com.softtown.modules.organization.entity.CompOrgDeptEntity;
import com.softtown.modules.organization.service.CompOrgDeptService;
import com.softtown.modules.organization.vo.CompOrgDeptTreeVO;
import com.softtown.modules.organization.vo.CompOrgDeptVO;
import com.softtown.modules.sys.enums.StatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version v1.0
 * @ProjectName: softtown-aioffice
 * @ClassName: CompOrgDeptController
 * @Description: 公司部门Controller层
 * @Author: fuming
 * @Date: 2019/6/4 18:07
 */
@RestController
@RequestMapping("/company/dept")
@Api(tags = "公司部门")
public class CompOrgDeptController {

    @Resource
    private CompOrgDeptService compOrgDeptService;

    /**
     * 根据所属公司的ID获取该公司下的所有部门信息
     *
     * @param companyId 公司id
     * @return com.softtown.common.utils.Result<java.util.List < com.softtown.modules.organization.vo.CompOrgDeptVO>>
     * @Author fuming
     * @Date 2020/4/13 20:22
     */
    @PostMapping("getCompOrgDept")
    @ApiOperation("获取公司下的部门信息")
    @LogOperation("获取公司下的部门信息")
    public Result<List<CompOrgDeptVO>> getCompOrgDeptList(@RequestBody String companyId) {
        //前台传过来是JSON格式，所以要转换
        String compid = JSONObject.fromObject(companyId).get("compId").toString();
        List<CompOrgDeptVO> deptListByCompId = compOrgDeptService.getOrgListByCompId(Long.valueOf(compid));
        return new Result<List<CompOrgDeptVO>>().ok(deptListByCompId);
    }

    /**
     * 获取公司下的部门信息【树形图】
     *
     * @param companyId 公司id
     * @return
     * @author Dingyayong
     * @date 2019/6/17 17:59
     */
    @PostMapping("getTreeDept")
    @ApiOperation("获取公司下的部门信息【树形图】")
    @LogOperation("获取公司下的部门信息【树形图】")
    public Result<List<CompOrgDeptVO>> getTreeDept(@RequestBody String companyId) {
        String compid = JSONObject.fromObject(companyId).get("compId").toString();
        List<CompOrgDeptVO> deptListByCompId = compOrgDeptService.getTreeDept(Long.valueOf(compid));
        return new Result<List<CompOrgDeptVO>>().ok(deptListByCompId);
    }

    /**
     * 获取公司下的部门信息【树形图】添加预算
     *
     * @param companyId 公司id
     * @return
     * @author Dingyayong
     * @date 2019/6/17 17:59
     */
    @PostMapping("getAddTreeDept")
    @ApiOperation("添加预算时获取公司下的部门信息【树形图】")
    @LogOperation("添加预算时获取公司下的部门信息【树形图】")
    public Result<List<CompOrgDeptVO>> getAddTreeDept(@RequestBody String companyId) {
        String compid = JSONObject.fromObject(companyId).get("compId").toString();
        List<CompOrgDeptVO> deptListByCompId = compOrgDeptService.getAddTreeDept(Long.valueOf(compid));
        return new Result<List<CompOrgDeptVO>>().ok(deptListByCompId);
    }

    /**
     * @param @param [compOrgDeptDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: addDepts
     * @Description: 部门添加
     * @author fuming
     * @date 2019/6/6 22:58
     */
    @PostMapping("addDept")
    @ApiOperation("添加部门")
    @LogOperation("添加部门")
    public Result addDepts(@RequestBody CompOrgDeptDTO compOrgDeptDTO) {
        Result result = new Result<>();
        //校验数据是否为空
        ValidatorUtils.validateEntity(compOrgDeptDTO, DefaultGroup.class);
        if (!RegularUtils.isNum(String.valueOf(compOrgDeptDTO.getSort()))) {
            return result.error(ErrorCode.INCORRECT_SORT_FORMAT);
        }
        List<CompOrgDeptVO> orgListByCompId = compOrgDeptService.getOrgListByCompId(compOrgDeptDTO.getCompId());
        for (CompOrgDeptVO orgDept:orgListByCompId) {
            String deptNames = orgDept.getName();
            if (compOrgDeptDTO.getName().equals(deptNames) && orgDept.getPid().equals(compOrgDeptDTO.getPid())) {
                return result.error(ErrorCode.DEPARTMENT_NAME_EXISTS);
            }
        }
        compOrgDeptService.addDepartment(compOrgDeptDTO);
        return result;
    }

    /**
     * @param @param [compOrgDeptDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: updateDeptInfo
     * @Description: 部门信息修改
     * @author fuming
     * @date 2019/6/7 23:16
     */
    @PostMapping("updateDeptInfos")
    @ApiOperation("修改部门信息")
    @LogOperation("修改部门信息")
    public Result updateDeptInfo(@RequestBody CompOrgDeptDTO compOrgDeptDTO) {
        Result result = new Result<>();
        //校验数据是否为空
        ValidatorUtils.validateEntity(compOrgDeptDTO, DefaultGroup.class);
        if (RegularUtils.isDeptName(compOrgDeptDTO.getName())) {
            return result.error(ErrorCode.INCORRECT_DEPARTMENT_FORMAT);
        }
        if (!RegularUtils.isNum(String.valueOf(compOrgDeptDTO.getSort()))) {
            return result.error(ErrorCode.INCORRECT_SORT_FORMAT);
        }
        if (compOrgDeptDTO.getPid().equals(compOrgDeptDTO.getDeptId())) {
            return result.error(ErrorCode.DEPARTMENT_NAME_EXISTS);
        }
        compOrgDeptService.updateDeptInfos(compOrgDeptDTO);
        return result;
    }

    /**
     * @param @param [compOrgDeptDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: deleteDepartment
     * @Description: 删除部门(逻辑删除)
     * @author fuming
     * @date 2019/6/24 22:04
     */
    @PostMapping("deleteDept")
    @ApiOperation("部门删除")
    @LogOperation("部门删除")
    public Result deleteDepartment(@RequestBody CompOrgDeptDTO compOrgDeptDTO) {
        Result result = new Result<>();
        CompOrgDeptVO deptInfoById = compOrgDeptService.getDeptInfoById(compOrgDeptDTO.getId());
        Long pid;
        List<CompOrgDeptVO> orgListByCompId = compOrgDeptService.getOrgListByCompId(compOrgDeptDTO.getCompId());
        for (int i = 0; i < orgListByCompId.size(); i++) {
            pid = orgListByCompId.get(i).getPid();
            if (deptInfoById != null && (deptInfoById.getId().equals(pid))) {
                return result.error(ErrorCode.PID_DATA_EXISTS);
            }
        }
        if (deptInfoById != null && deptInfoById.getEmplId() != null) {
            return result.error(ErrorCode.DATA_EXISTS);
        }
        if (deptInfoById != null && deptInfoById.getEmplDelId() != null) {
            return result.error(ErrorCode.DATA_EXISTS);
        } else {
            compOrgDeptService.deleteDepartment(compOrgDeptDTO.getId());
            return result;
        }
    }

    /**
     * 部门激活或禁用
     *
     * @param compOrgDeptDTO 公司部门DTO
     * @return com.softtown.common.utils.Result
     * @Author Dingyayong
     * @Date 2020/4/13 20:25
     */
    @PostMapping("deptActivate")
    @ApiOperation("部门激活或禁用")
    @LogOperation("部门激活或禁用")
    public Result deptActivate(@RequestBody CompOrgDeptDTO compOrgDeptDTO) {
        Result result = new Result<>();
        CompOrgDeptEntity compOrgDeptEntity = compOrgDeptService.getById(compOrgDeptDTO.getId());
        if (compOrgDeptEntity.getStatus().equals(StatusEnum.ACTIVATESTATUS.value())) {
            List<CompOrgDeptEntity> compOrgDeptEntities = compOrgDeptService.getDeptByPid(compOrgDeptDTO.getId());
            if (compOrgDeptEntities.size() > 0) {
                return result.error(ErrorCode.DEPT_EXISTS_ERROR);
            } else {
                compOrgDeptService.cancelOrgDept(compOrgDeptDTO.getId());
            }
        } else {
            compOrgDeptService.activeOrgDept(compOrgDeptDTO.getId());
        }
        return result;
    }

    /**
     * 获取部门名称信息
     *
     * @param deptId 部门id
     * @return com.softtown.common.utils.Result<com.softtown.modules.organization.entity.CompOrgDeptEntity>
     * @Author Dingyayong
     * @Date 2020/4/13 20:26
     */
    @PostMapping("getDeptName")
    @ApiOperation("获取部门名称信息")
    @LogOperation("获取部门名称信息")
    public Result<CompOrgDeptEntity> getDeptName(@RequestBody String deptId) {
        String id = JSONObject.fromObject(deptId).get("deptId").toString();
        CompOrgDeptEntity compOrgDeptEntity = compOrgDeptService.getById(Long.valueOf(id));
        return new Result<CompOrgDeptEntity>().ok(compOrgDeptEntity);
    }

    /**
     * 设置预算部门
     *
     * @param
     * @return
     * @author Dingyayong
     * @date 2019/7/30 9:43
     */
    @PostMapping("setBudDept")
    @ApiOperation("设置预算部门")
    @LogOperation("设置预算部门")
    public Result setBudDept(@RequestBody String deptId) {
        Result result = new Result<>();
        String id = JSONObject.fromObject(deptId).get("deptId").toString();
        CompOrgDeptEntity compOrgDeptEntity = compOrgDeptService.getById(Long.valueOf(id));
        if (compOrgDeptEntity.getIsBudDept().equals(StatusEnum.ACTIVATESTATUS.value())) {
            List<CompOrgDeptEntity> compOrgDeptEntities = compOrgDeptService.getDeptByPidAndBud(Long.valueOf(id));
            if (compOrgDeptEntities.size() > 0) {
                return result.error(ErrorCode.BUD_GET_DEPT);
            } else {
                compOrgDeptService.cancelBudGetDept(Long.valueOf(id));
            }
        } else {
            compOrgDeptService.setBudGetDept(Long.valueOf(id));
        }
        return result;
    }

    /**
     * @param @param [companyId]
     * @return @return com.softtown.common.utils.Result<java.util.List<com.softtown.modules.organization.vo.CompOrgDeptVO>>
     * @throws
     * @Title: getCompOrgBudDept
     * @Description: 获取公司下的预算部门信息
     * @author fuming
     * @date 2019/8/25 21:02
     */
    @PostMapping("getCompOrgBudDept")
    @ApiOperation("获取公司下的预算部门信息")
    @LogOperation("获取公司下的预算部门信息")
    public Result<List<CompOrgDeptVO>> getCompOrgBudDept(@RequestBody String companyId) {
        String compId = JSONObject.fromObject(companyId).get("compId").toString();
        List<CompOrgDeptVO> orgDeptVOList = compOrgDeptService.getBudDeptNameListByCompId(Long.valueOf(compId));
        return new Result<List<CompOrgDeptVO>>().ok(orgDeptVOList);
    }

    /**
     * 获取公司下的预算部门信息【树形图】
     *
     * @param companyId 公司id
     * @return com.softtown.common.utils.Result<java.util.List < com.softtown.modules.organization.vo.CompOrgDeptVO>>
     * @Author fuming
     * @Date 2020/4/13 20:26
     */
    @PostMapping("getBudgetTreeDept")
    @ApiOperation("获取公司下的预算部门信息【树形图】")
    @LogOperation("获取公司下的预算部门信息【树形图】")
    public Result<List<CompOrgDeptVO>> getBudgetTreeDept(@RequestBody String companyId) {
        String compid = JSONObject.fromObject(companyId).get("compId").toString();
        List<CompOrgDeptVO> deptListByCompId = compOrgDeptService.getBudgetDeptList(Long.valueOf(compid));
        return new Result<List<CompOrgDeptVO>>().ok(deptListByCompId);
    }

    /**
     * 获取公司下的预算部门信息【树形图】
     *
     * @param compOrgDeptTreeDTO 公司部门DTO
     * @return com.softtown.common.utils.Result<java.util.List < com.softtown.modules.organization.vo.CompOrgDeptTreeVO>>
     * @Author fuming
     * @Date 2020/4/13 20:27
     */
    @PostMapping("getBudgetTree")
    @ApiOperation("获取公司下的预算部门信息【树形图】")
    @LogOperation("获取公司下的预算部门信息【树形图】")
    public Result<List<CompOrgDeptTreeVO>> getBudgetTree(@RequestBody CompOrgDeptTreeDTO compOrgDeptTreeDTO) {
        ValidatorUtils.validateEntity(compOrgDeptTreeDTO);
        List<CompOrgDeptTreeVO> deptListByCompId = compOrgDeptService.getBudDeptList(compOrgDeptTreeDTO);
        return new Result<List<CompOrgDeptTreeVO>>().ok(deptListByCompId);
    }

    /**
     * 获取部门的上一部门名称
     *
     * @param compOrgDeptDTO 公司部门DTO
     * @return com.softtown.common.utils.Result
     * @Author fuming
     * @Date 2020/4/13 20:28
     */
    @PostMapping("getDeptPidName")
    @ApiOperation("获取部门的上一部门名称")
    @LogOperation("获取部门的上一部门名称")
    public Result getDeptPidName(@RequestBody CompOrgDeptDTO compOrgDeptDTO) {
        String deptPidName = compOrgDeptService.getDeptPidName(compOrgDeptDTO.getId());
        return new Result<>().ok(deptPidName);
    }
}
